package controllers

import (
	"encoding/json"
	"fmt"

	"github.com/astaxie/beego"
)

//结构体的字段首字母需要大写才能够被转换，
type Person struct {
	Name string `json:"name"`
	Age  int64  `json:"age"`
}

type PrintController struct {
	beego.Controller
}

func (this *PrintController) Get() {
	//	username := this.GetString("username")
	//	password := this.GetString("password")
	var s Person
	s.Age = 10
	s.Name = "123213"

	this.Data["json"] = s
	this.ServeJSON()

	//json转化 序列化
	bys, _ := json.Marshal(s)
	fmt.Println(string(bys))

	//json转化 反序列化
	//obj := &Person{}
	obj := new(Person)
	err := json.Unmarshal([]byte(bys), obj)
	if err != nil {
		fmt.Println("json error")
	}
	fmt.Println(obj.Age)

}
